
from datetime import datetime,date,timedelta, time
import empyrical as ep
import pandas as pd
import os ,sys
# current_file_path = os.path.abspath(__file__)
# father_path = os.path.abspath(os.path.dirname(current_file_path) + "/../")
# sys.path.append(father_path)
from ..visualization.plotting import Plot_ts,Plot_cs

class Backtest:

    def clip_returns_to_benchmark(self, rets, benchmark_rets):

        if (rets.index[0] < benchmark_rets.index[0]) or (rets.index[-1] > benchmark_rets.index[-1]):
            clipped_rets = rets[benchmark_rets.index]
        else:
            clipped_rets = rets
        return clipped_rets


    def performance_stats(self,returns):

        SIMPLE_STAT_FUNCS = [
            ep.annual_return,
            ep.cum_returns_final,
            ep.annual_volatility,
            ep.sharpe_ratio,
            ep.calmar_ratio,
            ep.max_drawdown
            ]

        stats = pd.Series()
        for stat_func in SIMPLE_STAT_FUNCS:
            stats[[stat_func.__name__]] = stat_func(returns)
        return stats

class Backtest_ts(Backtest):

    def backtest_ts_vectorization_01(self,md_data,signal):
        meta_data=md_data.copy()
        # close_diff=meta_data["close"].diff()
        close_diff = meta_data["close"] - meta_data["pre_close"]
        pnl_period =close_diff*signal.shift()
        pnl_period_cumsum=pnl_period.cumsum()
        meta_data['signal']= signal
        meta_data['pnl_period']=pnl_period
        meta_data['pnl_period_cumsum']=pnl_period_cumsum
        return meta_data

    def backtest_ts_vectorization_02(self, meta_data, signal):
        pass

    def backtest_ts_vectorization_03(self, meta_data, signal):
        pass
    
    def describer(self,meta_data):
        Plot_ts.plot_pnl_period_cumsum(meta_data=meta_data)

class Backtest_cs(Backtest):
    def __init__(self):
        pass

